<template>
  <div class="vuex-demo">
    <!-- 标题区域 -->
    <header class="demo-header">
      <h1>🚀 Vuex 学习示例集合</h1>
      <p>通过实际案例学习 Vuex 的核心概念和最佳实践</p>
    </header>
    
    <!-- 导航菜单 -->
    <nav class="demo-nav">
      <button 
        v-for="demo in demos" 
        :key="demo.id"
        @click="currentDemo = demo.id"
        :class="['nav-btn', { 'active': currentDemo === demo.id }]"
      >
        {{ demo.icon }} {{ demo.name }}
      </button>
    </nav>
    
    <!-- 示例内容区域 -->
    <main class="demo-content">
      <transition name="fade" mode="out-in">
        <component :is="currentDemoComponent" :key="currentDemo" />
      </transition>
    </main>
    
    <!-- 学习概览 -->
    <section class="learning-overview">
      <h2>📚 Vuex 核心概念概览</h2>
      <div class="concepts-grid">
        <div class="concept-card">
          <h3>🏪 State (状态)</h3>
          <p>应用的数据中心，单一数据源</p>
          <ul>
            <li>集中式状态管理</li>
            <li>响应式数据</li>
            <li>通过 this.$store.state 访问</li>
          </ul>
        </div>
        
        <div class="concept-card">
          <h3>🔧 Mutations (突变)</h3>
          <p>同步更改状态的唯一方法</p>
          <ul>
            <li>必须是同步函数</li>
            <li>通过 commit 调用</li>
            <li>便于调试和追踪</li>
          </ul>
        </div>
        
        <div class="concept-card">
          <h3>⚡ Actions (动作)</h3>
          <p>处理异步操作和复杂逻辑</p>
          <ul>
            <li>可包含异步操作</li>
            <li>通过 dispatch 调用</li>
            <li>提交 mutations 而不是直接变更状态</li>
          </ul>
        </div>
        
        <div class="concept-card">
          <h3>💰 Getters (计算属性)</h3>
          <p>基于状态派生的计算属性</p>
          <ul>
            <li>类似于组件的 computed</li>
            <li>具有缓存特性</li>
            <li>可以接受参数</li>
          </ul>
        </div>
        
        <div class="concept-card">
          <h3>📦 Modules (模块)</h3>
          <p>将 store 分割成模块</p>
          <ul>
            <li>避免单一状态树过于臃肿</li>
            <li>命名空间隔离</li>
            <li>便于大型应用管理</li>
          </ul>
        </div>
        
        <div class="concept-card">
          <h3>🔗 辅助函数</h3>
          <p>简化 Vuex 在组件中的使用</p>
          <ul>
            <li>mapState, mapGetters</li>
            <li>mapMutations, mapActions</li>
            <li>减少模板代码</li>
          </ul>
        </div>
      </div>
    </section>
    
    <!-- 最佳实践 -->
    <section class="best-practices">
      <h2>✨ Vuex 最佳实践</h2>
      <div class="practices-grid">
        <div class="practice-item">
          <h4>🎯 何时使用 Vuex</h4>
          <ul>
            <li>多个组件需要共享状态</li>
            <li>状态管理逻辑复杂</li>
            <li>需要时间旅行调试</li>
            <li>大型单页应用</li>
          </ul>
        </div>
        
        <div class="practice-item">
          <h4>🏗️ 项目结构建议</h4>
          <ul>
            <li>按功能模块划分 store</li>
            <li>使用常量定义 mutation 类型</li>
            <li>将复杂逻辑抽取到 actions</li>
            <li>合理使用命名空间</li>
          </ul>
        </div>
        
        <div class="practice-item">
          <h4>⚠️ 常见误区</h4>
          <ul>
            <li>不要在 mutations 中执行异步操作</li>
            <li>避免直接修改 state</li>
            <li>不要滥用 Vuex（简单状态用组件内部管理）</li>
            <li>注意模块间的数据依赖关系</li>
          </ul>
        </div>
        
        <div class="practice-item">
          <h4>🔧 调试技巧</h4>
          <ul>
            <li>使用 Vue DevTools</li>
            <li>合理命名 mutations 和 actions</li>
            <li>利用严格模式开发</li>
            <li>记录有意义的提交信息</li>
          </ul>
        </div>
      </div>
    </section>
  </div>
</template>

<script>
import CounterExample from './CounterExample.vue'
import TodoExample from './TodoExample.vue'
import UserExample from './UserExample.vue'
import ShoppingCartExample from './ShoppingCartExample.vue'
import DashboardExample from './DashboardExample.vue'

export default {
  name: 'VuexDemo',
  components: {
    CounterExample,
    TodoExample,
    UserExample,
    ShoppingCartExample
  },
  data() {
    return {
      currentDemo: 'counter',
      demos: [
        {
          id: 'counter',
          name: '计数器',
          icon: '🔢',
          description: '基础 State 和 Mutations 演示'
        },
        {
          id: 'todo',
          name: 'Todo列表',
          icon: '📝',
          description: '复杂 State 管理和 Getters 使用'
        },
        {
          id: 'user',
          name: '用户管理',
          icon: '👤',
          description: 'Actions 和异步操作演示'
        },
        {
          id: 'cart',
          name: '购物车',
          icon: '🛒',
          description: 'Modules 模块化管理演示'
        }
      ]
    }
  },
  computed: {
    currentDemoComponent() {
      const componentMap = {
        'counter': 'CounterExample',
        'todo': 'TodoExample',
        'user': 'UserExample',
        'cart': 'ShoppingCartExample'
      }
      return componentMap[this.currentDemo] || 'CounterExample'
    }
  }
}
</script>

<style scoped>
.vuex-demo {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.demo-header {
  text-align: center;
  color: white;
  margin-bottom: 30px;
}

.demo-header h1 {
  font-size: 2.5rem;
  margin-bottom: 10px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.demo-header p {
  font-size: 1.2rem;
  opacity: 0.9;
  margin: 0;
}

.demo-nav {
  display: flex;
  justify-content: center;
  gap: 15px;
  margin-bottom: 30px;
  flex-wrap: wrap;
}

.nav-btn {
  padding: 12px 24px;
  border: none;
  border-radius: 25px;
  background: rgba(255, 255, 255, 0.9);
  color: #495057;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
}

.nav-btn:hover {
  background: rgba(255, 255, 255, 1);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.nav-btn.active {
  background: #fff;
  color: #667eea;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  transform: translateY(-2px);
}

.demo-content {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  padding: 0;
  margin-bottom: 40px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter, .fade-leave-to {
  opacity: 0;
}

.learning-overview {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  padding: 30px;
  margin-bottom: 30px;
  backdrop-filter: blur(10px);
}

.learning-overview h2 {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 30px;
  font-size: 1.8rem;
}

.concepts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
}

.concept-card {
  background: #fff;
  border-radius: 10px;
  padding: 25px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  border-left: 4px solid #667eea;
}

.concept-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.concept-card h3 {
  color: #495057;
  margin-bottom: 15px;
  font-size: 1.2rem;
}

.concept-card p {
  color: #6c757d;
  margin-bottom: 15px;
  line-height: 1.5;
}

.concept-card ul {
  list-style: none;
  padding: 0;
}

.concept-card li {
  color: #495057;
  padding: 5px 0;
  position: relative;
  padding-left: 20px;
}

.concept-card li:before {
  content: '▸';
  color: #667eea;
  position: absolute;
  left: 0;
}

.best-practices {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  padding: 30px;
  backdrop-filter: blur(10px);
}

.best-practices h2 {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 30px;
  font-size: 1.8rem;
}

.practices-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 20px;
}

.practice-item {
  background: #fff;
  border-radius: 10px;
  padding: 25px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.practice-item:hover {
  transform: translateY(-3px);
}

.practice-item h4 {
  color: #495057;
  margin-bottom: 15px;
  font-size: 1.1rem;
}

.practice-item ul {
  list-style: none;
  padding: 0;
}

.practice-item li {
  color: #6c757d;
  padding: 8px 0;
  border-bottom: 1px solid #f1f3f4;
  position: relative;
  padding-left: 20px;
  line-height: 1.4;
}

.practice-item li:last-child {
  border-bottom: none;
}

.practice-item li:before {
  content: '✓';
  color: #28a745;
  position: absolute;
  left: 0;
  font-weight: bold;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .demo-header h1 {
    font-size: 2rem;
  }
  
  .demo-nav {
    gap: 10px;
  }
  
  .nav-btn {
    padding: 10px 16px;
    font-size: 0.9rem;
  }
  
  .concepts-grid,
  .practices-grid {
    grid-template-columns: 1fr;
  }
  
  .learning-overview,
  .best-practices {
    padding: 20px;
  }
}
</style>
